package com.taotao.service.impl;

import com.taotao.HttpUtil;
import com.taotao.JsonUtils;
import com.taotao.TaotaoResult;
import com.taotao.pojo.TbContent;
import com.taotao.service.TbContentService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author evader
 * @date 2020-09-27 17:08
 */
@Service
public class TbContentServiceImpl implements TbContentService {


    @Override
    public String getContentList() {
        {
            //调用服务层的服务
            String result = HttpUtil.doGet("http://localhost:8081/content/dgg");

            try {
                TaotaoResult taotaoResult = TaotaoResult.formatToList(result, TbContent.class);
                //内容列表
                List<TbContent> list = (List<TbContent>) taotaoResult.getData();
                List<Map> mapList = new ArrayList<>();
                for (TbContent tbContent : list){
                    Map map = new HashMap();
                    map.put("src", tbContent.getPic());
                    map.put("height", 240);
                    map.put("width", 670);
                    map.put("srcB", tbContent.getPic2());
                    map.put("widthB", 550);
                    map.put("heightB", 240);
                    map.put("href", tbContent.getUrl());
                    map.put("alt", tbContent.getSubTitle());
                    mapList.add(map);
                }
                return JsonUtils.objectToJson(mapList);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

    }
}
